/**
 * Created by George on 2015-06-29.
 */
'use strict';
console.log('Load O2O: ' + module.id);
let F = require('./function');
let Dao = require('./dao');
let districtDispatcher = new Dao.district();

function* api() {
}
exports.api = api;


/**
 * @todo 添加
 * @param Env
 */
api.add = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield districtDispatcher.add(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * @todo 删除地区接口
 *  post /o2o_service/district/v1/delete/
 * {
 *  "code":100,
 * }
 * @param Env
 */
api.delete = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  if (!fields.code) return F.returnMsg(400, '必须有code', 3);

  let ret = yield districtDispatcher.delete(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, 'OK', 3, result);
};

/**
 * 根据条件查找地区
 *  post /o2o_service/district/v1/info/
 * {
 *  "code":100,
 *  "type":"tree", // tree 整棵树,single 单个节点,directChildren 直接child, allChildren 所有child
 *  "act":1
 * }
 */
api.info = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  var fields = Env.I;
  //参数检测
  if (!fields.code) return F.returnMsg(400, 'error', 3);

  let ret = yield  districtDispatcher.info(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * 搜索附近的地区
 *  post /o2o_service/district/v1/near/
 *  查询 [距离(11,22)在2000m范围内的理发店
 {
    "loc":[11,22],
    "radius":1000,// default
    "pageSize": 5,
    "page": 1,
    "act":1
 }
 */
api.near = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield  districtDispatcher.near(fields);
  if (ret.error == 1) return F.returnMsg(400, ret.msg, 3, ret.data);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * 获取城市列表
 *  post /o2o_service/district/v1/city/
 * 查询城市
 {
    "pCode":1,//可选,没有此字段,则查询全国城市列表
    "act":1
 }
 * @param Env
 */
api.city = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield districtDispatcher.city(fields);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};

/**
 * 获取省+直辖市
 *  {
        "act":1
    }
 * @param Env
 */
api.province = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield districtDispatcher.province(fields);
  return F.returnMsg(200, ret.msg, 3, ret.data)
};

api.list = function*(Env) {
  Env.I = yield F.Init(Env, [1]);
  if (Env.I.errors) {
    return F.returnMsg(400, Env.I.errors.msg, Env.I.errors.level);
  }
  let fields = Env.I;

  let ret = yield districtDispatcher.list(fields);
  return F.returnMsg(200, ret.msg, 3, ret.data);
};